package com.feishi.core.util.common;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2019-04-28
 * Time: 上午9:32
 */
public class EquidistantLine {

    /**
     * 正弦函数等距线计算归纳函数
     * x1,y1起始坐标位置
     * x2,y2终端坐标位置
     * xAccuracy函数精度,生成数组x坐标的密度
     * distance等距距离
     * float[i][j]返回值
     * float[0][j]为x轴为j*xAccuracy中心点的y坐标
     * float[1][j]为x轴为j*xAccuracy上边界的y坐标
     * float[2][j]为x轴为j*xAccuracy下边界的y坐标
     * */



    public float[][] getBorder(int x1,float y1,int x2,float y2,float xAccuracy,float distance){
        int T= (int) ((x2-x1)/xAccuracy);
        float k=(y2-y1)/2;
        float[][] fff=new float[3][T+1];
        /**
         *计算等距点位置
         */
        for (int i = 0 ; i <= T; i++) {
            float x=i*xAccuracy;
            float y= (float)(-k*Math.cos(x*1f/xAccuracy/T*Math.PI)+(y1+y2)/2);

            float dty= (float) (k*1f/T/xAccuracy*Math.PI*Math.sin(x*1f/xAccuracy/T*Math.PI));
            float _x = (float) (-dty / Math.sqrt(1 + dty * dty) * distance);
            float _y = (float) (1 / Math.sqrt(1 + dty * dty) * distance);
            int iUp= (int) ((x + _x)/xAccuracy);
            iUp=iUp<0?0:(T<iUp?T:iUp);//越界警告
            int iDown= (int) ((x - _x)/xAccuracy);
            iDown=iDown<0?0:(T<iDown?T:iDown);//越界警告
            fff[0][i]=y;
            fff[1][iUp]=y+_y;
            fff[2][iDown]=y-_y;
        }
        //对等距点数组补缺
        float ny1=0;
        float ny2=0;
        for (int x = 0 ; x <= T; x++) {
            fff[1][x]=fff[1][x]==0?ny1:fff[1][x];
            ny1=fff[1][x];
            fff[2][x]=fff[2][x]==0?ny2:fff[2][x];
            ny2=fff[2][x];
        }
        return fff;
    }
}
